package com.ybbase.framework.config.activiti.custom.user;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.ybbase.framework.config.activiti.WorkflowConstants;
import com.ybbase.framework.config.activiti.custom.util.ActivitiUserUtil;
import com.ybbase.framework.model.po.system.Organization;
import com.ybbase.framework.model.po.system.User;
import com.ybbase.framework.service.system.OrganizationService;
import com.ybbase.framework.service.system.UserService;
import org.activiti.engine.identity.Group;
import org.activiti.engine.impl.Page;
import org.activiti.engine.impl.UserQueryImpl;
import org.activiti.engine.impl.persistence.entity.IdentityInfoEntity;
import org.activiti.engine.impl.persistence.entity.UserEntity;
import org.activiti.engine.impl.persistence.entity.UserEntityManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;
/**
 * @author cg
 * @date 2019-08-22 15:00
 * @DEC 继承UserEntityManager类
 */
@Component
public class CustomUserEntityManager extends UserEntityManager{

    @Autowired
    @Lazy
    private UserService userService;
    @Autowired
    @Lazy
    private OrganizationService organizationService;
    @Override
    public UserEntity findUserById(String userId) {
        User user = getUser(userId);
        return ActivitiUserUtil.toActivitiUser(user);
    }

    @Override
    public List<Group> findGroupsByUser(String userId) {
        if (userId == null) {
            return null;
        }
        List<String> listStr = new ArrayList<>();
        try {
            List<Organization> list = organizationService.getOrgListByUserId(Integer.valueOf(userId));
            if(list!=null && list.size()>0){
                list.forEach(Organization ->{listStr.add(Organization.getId()+"");});
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        List<Group> gs = ActivitiUserUtil.toActivitiGroups(listStr);
        return gs;
    }

    @Override
    public List<org.activiti.engine.identity.User> findUserByQueryCriteria(UserQueryImpl query, Page page) {
        User user = getUser(query.getId());
        List<org.activiti.engine.identity.User> list = new ArrayList<>();
        list.add(ActivitiUserUtil.toActivitiUser(user));
        return list;
    }

    private User getUser(String userId) {
        User user = new User();
        if(WorkflowConstants.INTERFACE_SYSTEM_ID.equals(userId)) {
            user.setId(Integer.valueOf(WorkflowConstants.INTERFACE_SYSTEM_ID));
            user.setLoginName(WorkflowConstants.INTERFACE_SYSTEM_NAME);
            user.setName(WorkflowConstants.INTERFACE_REAL_NAME);
            user.setPassword("");
            user.setEmail("");
        }else {
            try {
                user = userService.getById(userId);
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        return user;
    }

    @Override
    public long findUserCountByQueryCriteria(UserQueryImpl query) {
        throw new RuntimeException("not implement method.");
    }

    @Override
    public IdentityInfoEntity findUserInfoByUserIdAndKey(String userId, String key) {
        throw new RuntimeException("not implement method.");
    }

    @Override
    public List<String> findUserInfoKeysByUserIdAndType(String userId, String type) {
        throw new RuntimeException("not implement method.");
    }

    @Override
    public List<org.activiti.engine.identity.User> findPotentialStarterUsers(String proceDefId) {
        throw new RuntimeException("not implement method.");
    }

    @Override
    public List<org.activiti.engine.identity.User> findUsersByNativeQuery(Map<String, Object> parameterMap,
                                                                          int firstResult, int maxResults) {
        throw new RuntimeException("not implement method.");
    }

    @Override
    public long findUserCountByNativeQuery(Map<String, Object> parameterMap) {
        throw new RuntimeException("not implement method.");
    }

}
